package com.atguigu.tingshu.album.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.atguigu.tingshu.album.mapper.*;
import com.atguigu.tingshu.album.service.BaseCategoryService;
import com.atguigu.tingshu.model.album.BaseAttribute;
import com.atguigu.tingshu.model.album.BaseCategory1;
import com.atguigu.tingshu.model.album.BaseCategory3;
import com.atguigu.tingshu.model.album.BaseCategoryView;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.function.BiConsumer;
import java.util.function.Function;
import java.util.stream.Collectors;

@Service
@SuppressWarnings({"unchecked", "rawtypes"})
public class BaseCategoryServiceImpl extends ServiceImpl<BaseCategory1Mapper, BaseCategory1> implements BaseCategoryService {

    @Autowired
    private BaseCategory1Mapper baseCategory1Mapper;

    @Autowired
    private BaseCategory2Mapper baseCategory2Mapper;

    @Autowired
    private BaseCategory3Mapper baseCategory3Mapper;

    @Autowired
    private BaseCategoryViewMapper baseCategoryViewMapper;

    @Autowired
    private BaseAttributeMapper baseAttributeMapper;

    @Override
    public List<JSONObject> getBaseCategoryList() {
        //三级分类嵌套集合
        List<JSONObject> jsonObjects = new ArrayList<>();
        //查询三级分类列表
        List<BaseCategoryView> allCategoryList = baseCategoryViewMapper.selectList(null);
        if (CollectionUtils.isEmpty(allCategoryList)) {
            return null;
        }
        //一级分类
        Map<Long, List<BaseCategoryView>> mapGroupByCategory1Id = allCategoryList.stream().collect(Collectors.groupingBy(BaseCategoryView::getCategory1Id));
        mapGroupByCategory1Id.forEach((category1Id, category2List) -> {
            //二级分类列表
            List<JSONObject> jsonObjects2 = new ArrayList<>();
            JSONObject jsonObject1 = new JSONObject();
            jsonObjects.add(jsonObject1);
            jsonObject1.put("categoryName", category2List.get(0).getCategory1Name());
            jsonObject1.put("categoryId", category1Id);
            jsonObject1.put("categoryChild", jsonObjects2);
            Map<Long, List<BaseCategoryView>> mapGroupByCategory2Id = category2List.stream().collect(Collectors.groupingBy(BaseCategoryView::getCategory2Id));
            mapGroupByCategory2Id.forEach((category2Id, category3List) -> {
                JSONObject jsonObject2 = new JSONObject();
                jsonObjects2.add(jsonObject2);
                jsonObject2.put("categoryName", category3List.get(0).getCategory2Name());
                jsonObject2.put("categoryId", category2Id);
                jsonObject2.put("categoryChild", category3List.stream().map(baseCategoryView -> {
                            JSONObject jsonObject3 = new JSONObject();
                            jsonObject3.put("categoryName", baseCategoryView.getCategory3Name());
                            jsonObject3.put("categoryId", baseCategoryView.getCategory3Id());
                            return jsonObject3;
                        }).collect(Collectors.toList())
                );
            });
        });
        return jsonObjects;
    }


    @Override
    public List<BaseAttribute> findAttribute(Long category1Id) {
        return baseAttributeMapper.findAttribute(category1Id);
    }

    @Override
    public List<BaseCategory3> getBaseCategory3List(Long category1Id) {
        //根据一级分类id查询三级的top6的category3的信息
        return baseCategory3Mapper.getBaseCategory3List(category1Id);
    }

    @Override
    public JSONObject getBaseCategoryList2(Long category1Id) {
        //创建返回值对象
        JSONObject jsonObject = new JSONObject();
        //查询一级分类的子列表
        List<BaseCategoryView> baseCategoryViews = baseCategoryViewMapper.selectList(new LambdaQueryWrapper<BaseCategoryView>().eq(BaseCategoryView::getCategory1Id, category1Id));
        //如果为空，直接返回
        if (CollectionUtils.isEmpty(baseCategoryViews)) {
            return null;
        }
        //一级分类数据
        ArrayList<JSONObject> jsonObjects2 = new ArrayList<>();
        jsonObject.put("categoryName", baseCategoryViews.get(0).getCategory1Name());
        jsonObject.put("categoryId", category1Id);
        jsonObject.put("categoryChild", jsonObjects2);

        //根据category2Id分组
        Map<Long, List<BaseCategoryView>> baseCategoryViews2 = baseCategoryViews.stream().collect(Collectors.groupingBy(BaseCategoryView::getCategory2Id));
        baseCategoryViews2.forEach((category2Id, category3List) -> {
            JSONObject jsonObject2 = new JSONObject();
            jsonObjects2.add(jsonObject2);
            jsonObject2.put("categoryName", category3List.get(0).getCategory2Name());
            jsonObject2.put("categoryId", category2Id);
            jsonObject2.put("categoryChild", category3List.stream().map(category3 -> {
                JSONObject jsonObject3 = new JSONObject();
                jsonObject3.put("categoryName", category3.getCategory3Name());
                jsonObject3.put("categoryId", category3.getCategory3Id());
                return jsonObject3;
            }).collect(Collectors.toList()));
        });
        return jsonObject;
    }
}
